<?php

class Update_model extends Model {

    function Update_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    /**
     * Returns the current version of the database
     */
    function get_db_version() {
    	$this->db->where('name', 'db_version');
    	$query = $this->db->get('cr_config');
    	$array = $query->row_array();
    	return $array['value'];
    }
    
    /**
     * Returns a massive array containing all informations about packages, branches, relations and releases
     * which have appeared since the $fromversion version of the database
     */
    function get_new_packages($fromversion) {
    	if (!is_numeric($fromversion))
    		return false;
    	static $fld_renaming = array(
			'cr_packages.name' => 'pkg_name',
    		'cr_packages.status' => 'pkg_status',
    		'cr_branches.name' => 'bch_name',
    		'cr_branches.status' => 'bch_status',
    		'cr_releases.status' => 'rls_status',
    		'cr_packages.last_update' => 'pkg_last_up',
    		'cr_branches.last_update' => 'bch_last_up',
    		'cr_releases.last_update' => 'rls_last_up',
    		'cr_branches.creation_date' => 'bch_creation',
    		'cr_packages.creation_date' => 'pkg_creation');
    	$fldlist = '';
    	foreach ($fld_renaming as $key => $value)
    		$fldlist .= ','.$key.' as '.$value;
    	$querystr = 'SELECT * '.$fldlist.' FROM cr_packages ' .
    			'LEFT JOIN cr_branches ON cr_packages.pkg_id = cr_branches.package ' .
    			'LEFT JOIN cr_releases ON cr_branches.branch_id = cr_releases.branch ' .
    			'LEFT JOIN cr_bch_relations ON cr_branches.branch_id = cr_bch_relations.bch1 ' .
    			'WHERE cr_packages.db_version > '.$fromversion.
    			' OR cr_branches.db_version > ' .$fromversion.
    			' OR cr_bch_relations.db_version > '.$fromversion.
    			' OR cr_releases.db_version > '.$fromversion.
    			' ORDER BY cr_packages.pkg_id, cr_branches.branch_id';
    	$query = $this->db->query($querystr);
    	return $query->result_array();
    }

}

?>
